<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:54
 */

namespace App\Http\Services;


use App\Http\Model\ColorGiftRateConfig;
use App\Http\Model\FinanceCharm;
use App\Http\Model\FinanceRoomWallet;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\Gift;
use App\Http\Model\Guard;
use App\Http\Model\Mall;
use App\Http\Model\PropEvent;
use App\Http\Model\Room;
use App\Http\Model\RoomHeart;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\RoomHot;
use App\Http\Model\RoomMelee;
use App\Http\Model\RoomTally;
use App\Http\Model\RoomUser;
use App\Http\Model\System;
use App\Http\Model\User;
use App\Http\Model\UserProp;
use App\Lib\rc;
use App\Http\Model\Medal as MedalModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Services\NameplateServices;

class RoomMsgServices extends CommonServices
{
    private $model_user;
    private $model_mall;
    private $model_finance_user_wallet;
    private $model_finance_room_wallet;
    private $lib_rong;
    private $model_room_user;
    private $model_room_hot;
    private $medalModel;
    private $model_room;
    protected $modelRoomMelee;
    protected $modelRoomTally;
    protected $modelRoomHeart;
    protected $propEventModel;
    protected $userPropModel;
    protected $ratioConf;
    protected $modelGift;
    protected $modelSystem;
    protected $modelFinanceCharm;
    protected $colorGiftModel;
    protected $MsgServie;
    protected $nameplateService;


    protected $userSendColorGiftInfo = 'COLOR:GIFT:SEND:CHARMINFO:';//zset 用户送礼主播魅力值列表 COLOR:GIFT:SEND:CHARMINFO:{userId} => {int} {userid}
    protected $userAwadColorGiftInfo = 'COLOR:GIFT:AWAD:LIST:';//zset 用户送礼主播收礼列表 COLOR:GIFT:AWAD:LIST:{userId} => {int} {giftNmae - userid}
    protected $userSendColorGiftList = 'COLOR:GIFT:SEND:LIST:';//zset 用户获得礼物列表 COLOR:GIFT:SEND:LIST:{userId} => {int} {giftname}
    protected $colorGiftRateRedis = 'COLOR:GIFT:RATE:LIST:'; //string 彩蛋奖池列表 COLOR:GIFT:RATE:LIST:{giftID}=>{json}

    public function __construct()
    {
        $this->model_user = new User();
        $this->model_mall = new Mall();
        $this->model_finance_room_wallet = new FinanceRoomWallet();
        $this->model_finance_user_wallet = new FinanceUserWallet();
        $this->lib_rong = new rc();
        $this->model_room_user = new RoomUser();
        $this->model_room_hot = new RoomHot();
        $this->medalModel = new MedalModel();
        $this->model_room = new Room();
        $this->modelRoomMelee = new RoomMelee();
        $this->modelRoomTally = new RoomTally();
        $this->modelRoomHeart = new RoomHeart();
        $this->propEventModel = new PropEvent();
        $this->userPropModel = new UserProp();
        $this->ratioConf = config('pay.wallet_change_ratio');
        $this->modelGift = new Gift();
        $this->modelSystem = new System();
        $this->modelFinanceCharm = new FinanceCharm();
        $this->colorGiftModel = new ColorGiftRateConfig();
        $this->MsgServie = new MsgServices();
        $this->nameplateService = new NameplateServices();

    }

    /**
     * 群文本消息
     */
    public function text($room_id, $text)
    {

    }

    /**
     * 群图片消息
     * @param $room_id
     * @param $pic_url
     */
    public function pic($room_id, $pic_url)
    {

    }

    /**
     * 发送系统消息 仅支持文字
     * @param $room_id
     * @param $text
     * @param $to_user_id
     */
    public function system($room_id, $text, $to_user_id = null)
    {
        $data = [
            "k" => 10003,
            "t" => $text,
            "tid" => $to_user_id
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 礼物消息
     * @param $room_id
     * @param $send_user_id
     * @param $recv_user_ids
     * @param $gift_id
     * @param $gift_count
     */
    public function broadcast_gift($room_id, $send_user_id, $recv_user_ids, $gift_id, $gift_count, $at = 0, $flag = '', $giftPrice = 0 ,$skill_data = null ,$track_json = null, $extend=[])
    {
        // write_log('RoomMsgServices'.(isCli()?'-cli':''), 'broadcast_gift', func_get_args());
        $send_user = $this->model_user->get_user($send_user_id);
        $recv_users = $this->model_user->get_users_by_ids($recv_user_ids);
        $wallet = $this->model_finance_user_wallet->get_wallet($send_user_id);
        $recv_wallet = $this->model_finance_user_wallet->get_wallets($recv_user_ids);
        $room_wealth_value = $this->model_finance_room_wallet->get_wallet($room_id);
        $giftData = $this->modelGift->formatGiftInfo($this->modelGift->get_gift($gift_id), $gift_count, $send_user_id);
        // write_log('RoomMsgServices'.(isCli()?'-cli':''), 'recv_users', $recv_users);


        $liang = $send_user['liang'] ?? 0;
        $super = $send_user['super'] ?? 0;
        $official = $send_user['official'] ?? 0;
        $waiter = $send_user['waiter'] ?? 0;
        $novice = $send_user['novice'] ?? 0;
        $v = $send_user['v'] ?? 0;

        $recvs = [];
        $recv_wallet = array_column($recv_wallet, null, 'user_id');
        $wealthTotal = intval($giftPrice * $gift_count * ($this->ratioConf['diamond_to_wealth'] ?? 0.1));
        $charmTotal = intval($giftPrice * $gift_count * ($this->ratioConf['diamond_to_charm'] ?? 0.1));

        $isSpecialGift = $giftData['is_special'] ?? 0;
        $sendGift = [];
        $allAwardData = $extend;
        if(!empty($allAwardData)){
            foreach($allAwardData as $k=>$t){
                foreach($t as $value){
                    if(isset($value['colorInfo'])){
                        $sendGift[$k][] = [
                            'gift_name' => $value['gift_name'].'('.$value['gift_price'].'钻石)',
                            'to_user' => $k ,
                            'gift_num' => $value['pop_num'],
                            'gift_all' => $value['gift_price'] * $value['pop_num']
                        ];
                    }
                }
            }
        } 
        for ($i = 0; $i < count($recv_users); $i++) {
            $item = [];
            $item['id'] = $recv_users[$i]['user_id'];
            $item['nn'] = $recv_users[$i]['nickname'];
            $item['cc'] = $recv_wallet[$recv_users[$i]['user_id']]['charm_balance'];
            $item['rc'] = $recv_wallet[$recv_users[$i]['user_id']]['reputation_balance'];
            if($giftData['gift_type'] == 5){
                $charmTotal = $sendGift[$recv_users[$i]['user_id']]['gift_all'];
            }
            $item['c_incr'] = $charmTotal;
            $item['rimg'] = '';
            $item['iscp'] = 0;
            $item['sendGift'] = isset($sendGift[$recv_users[$i]['user_id']]) ? $sendGift[$recv_users[$i]['user_id']] :[];
            if($isSpecialGift) $item['rimg'] = $recv_users[$i]['images'][0] ?? User::USER_DEFAULT_IMAGE[0];
            array_push($recvs, $item);
        }
        // write_log('RoomMsgServices'.(isCli()?'-cli':''), 'broadcast_gift', $recvs);
        $fimg = null;
        if (count($send_user["images"]) > 0) {
            $fimg = $send_user["images"][0];
            if (isset($send_user["thumbnails"]) && count($send_user["thumbnails"]) > 0) {
                $fimg = $send_user["thumbnails"][0];
            }
        }

        //用户铭牌
        $np = [];
        $userNameplate = $this->nameplateService->getNameplateByUserId($send_user_id);
        foreach($userNameplate as $vn) {
            if(!empty($vn['nameplate_resource'])) {
                $np[] = $vn['nameplate_resource'];
                continue;
            }
            if(!empty($vn['nameplate_img'])) {
                $np[] = $vn['nameplate_img'];
                continue;
            }
        }

        $data = [
            "k" => 20004,
            "fid" => intval($send_user_id),
            "fnn" => $send_user['nickname'],
            "fimg" => $fimg,
            "fsex" => $send_user['gender'],
            "fbt" => $send_user["birthday"],
            "tu" => $recvs,
            "gid" => intval($gift_id),
            "flag" => $flag,
            "gc" => intval($gift_count),
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),
            "rc" => intval($wallet['reputation_balance']),
            "bc" => intval($wallet['celebrity_balance']),
            "gameb" => empty(intval($wallet['game_balance'])) ? 0 : intval($wallet['game_balance']),
            "rwc" => $room_wealth_value,
            "at" => $at,
            "l" => $liang,
            "s" => $super,
            "o" => $official,
            "w" => $waiter,
            "v" => $v,
            'nov' => $novice,
            'medal' => [],//勋章
            'gsm' => null,//守护
            'w_incr' => $wealthTotal,
            'gift_name' => $giftData['name'],
            'gift_price' => $giftPrice,
            'gift_image' => $giftData['image'] ?? '',
            'gift_resource' => $giftData['animate_resource'] ?? '',
            'gift_resource_md5' => $giftData['animate_resource_md5'] ?? '',
            'gift_type' => empty($giftData['gift_type']) ? 0 : $giftData['gift_type'],
            'te' => $isSpecialGift,                                      // 1 特殊礼物
            //是否隐藏页面信息
            "hide_msg" => isset($extend['hide_msg']) ? $extend['hide_msg'] : 0,
            //(只针对房间送礼)1公屏消息 2礼物全屏大动画 3最近一条礼物消息 4左上角礼物横条 5礼物麦位小动画
            "mission" => isset($extend['mission']) ? $extend['mission'] : '1,2,3,4,5', 
            'np' => $np,//用户铭牌

        ];
        
        //彩蛋礼物特殊处理
        if($giftData['gift_type'] == 5){
            Redis::DEL($this->userSendColorGiftInfo.$send_user_id);
            Redis::DEL($this->userAwadColorGiftInfo.$send_user_id);
        }
        if(!empty($track_json)){
            $data['track_json'] = $track_json;
        }
        // write_log('Ceshi','log',json_encode($data));
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 主持麦上麦指令
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function command_hostmicro_into($room_id, $micro_id, $user_id)
    {
        //检查用户禁麦状态
        $microBlockStatus = $this->model_user->getMicroBlockStatus($user_id);

        $data = [
            "k" => 30001,
            "mid" => intval($micro_id),
            "uid" => intval($user_id),
            'mb' => $microBlockStatus ? 1 : 0,
        ];
        $this->send_room_msg($room_id, $data);

        $this->model_room->addMicroLog($room_id, $micro_id, $user_id,1);
    }

    /**
     * 主持麦上麦通知
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function broadcast_hostmicro_into($room_id, $micro_id, $user_id)
    {
        $attires = $this->model_mall->getGoodsAttire($user_id);
        $cid = null;
        $cres = null;
        for ($i = 0; $i < count($attires); $i++) {
            if ($attires[$i]['goods_type'] == 1) {
                $cid = $attires[$i]['goods_id'];
                $cres = $attires[$i]['animate_resource'];
            }
        }
        $user = $this->model_user->get_user($user_id);
        $mid = $micro_id;
        $uid = $user_id;
        $nn = $user['nickname'];
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
        }
        $sex = $user["gender"];
        $bt = $user["birthday"];
        $v = $user['v'] ?? 0;

        //检查用户禁麦状态
        $microBlockStatus = $this->model_user->getMicroBlockStatus($user_id);

        $data = [
            "k" => 30002,
            "mid" => $mid,
            "uid" => $uid,
            "nn" => $nn,
            "img" => $img,
            "sex" => $sex,
            "bt" => $bt,
            "cid" => $cid,
            "cres" => $cres,
            'v' => $v,
            'mb' => $microBlockStatus ? 1 : 0,
        ];

        $this->send_room_msg($room_id, $data);
    }

    /**
     * 主持麦下麦指令
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function command_hostmicro_out($room_id, $micro_id, $user_id)
    {  //文档需要修改
        $data = [
            "k" => 30003,
            "mid" => intval($micro_id),
            "uid" => intval($user_id)
        ];
        $this->send_room_msg($room_id, $data);

        $this->model_room->addMicroLog($room_id, $micro_id, $user_id,2);
    }

    /**
     * 主持麦下麦通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_hostmicro_out($room_id, $micro_id, $timestamp, $userId = 0)
    {
        $data = [
            "k" => 30004,
            "mid" => intval($micro_id),
            "cts" => intval($timestamp)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 主持麦闭麦指令
     * @param $room_id
     * @param $micro_id
     */
    public function command_hostmicro_volume_close($room_id, $micro_id)
    {
        $data = [
            "k" => 30059,
            "mid" => intval($micro_id)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 主持麦闭麦通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_hostmicro_volume_close($room_id, $micro_id)
    {
        $data = [
            "k" => 30005,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 主持麦开麦指令
     * @param $room_id
     * @param $micro_id
     */
    public function command_hostmicro_volume_open($room_id, $micro_id)
    {
        $data = [
            "k" => 30060,
            "mid" => intval($micro_id)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 主持麦开麦通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_hostmicro_volume_open($room_id, $micro_id)
    {
        $data = [
            "k" => 30006,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦上麦指令
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     * @param $type 0.正常上麦、1.抱上麦
     */
    public function command_micro_into($room_id, $micro_id, $user_id, $type = 0)
    {
        $data = [
            "k" => 30007,
            "mid" => intval($micro_id),
            "uid" => intval($user_id),
            "t" => intval($type)
        ];
        $this->send_room_msg($room_id, $data);

        $this->model_room->addMicroLog($room_id, $micro_id, $user_id,1);
    }


    /**
     * 普通麦上麦通知
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function broadcast_micro_into($room_id, $micro_id, $user_id)
    {
        $attires = $this->model_mall->getGoodsAttire($user_id);
        $cid = null;
        $cres = null;
        for ($i = 0; $i < count($attires); $i++) {
            if ($attires[$i]['goods_type'] == 1) {
                $cid = $attires[$i]['goods_id'];
                $cres = $attires[$i]['animate_resource'];
            }
        }
        $user = $this->model_user->get_user($user_id);
        $mid = $micro_id;
        $uid = $user_id;
        $nn = $user['nickname'];
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
        }
        $sex = $user["gender"];
        $bt = $user["birthday"];
        $v = $user['v'] ?? 0;
        $data = [
            "k" => 30008,
            "mid" => $mid,
            "uid" => $uid,
            "nn" => $nn,
            "img" => $img,
            "sex" => $sex,
            "bt" => $bt,
            "cid" => $cid,
            "cres" => $cres,
            'v' => $v
        ];

        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦下麦指令
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function command_micro_out($room_id, $micro_id, $user_id)
    {
        $data = [
            "k" => 30009,
            "mid" => intval($micro_id),
            "uid" => intval($user_id),
        ];
        $this->send_room_msg($room_id, $data);

        $this->model_room->addMicroLog($room_id, $micro_id, $user_id,2);
    }

    /**
     * 批量下麦指令
     * @param $room_id
     * @param $userIdArr
     */
    public function command_micro_out_arr($room_id, $userIdArr)
    {
        $data = [
            "k" => 30076,
            "uids" => $userIdArr,
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 普通麦下麦通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_micro_out($room_id, $micro_id, $userId = 0)
    {
        $data = [
            "k" => 30010,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 普通麦禁用通知
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function broadcast_micro_lock($room_id, $micro_id)
    {
        $data = [
            "k" => 30011,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦取消禁用通知
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function broadcast_micro_unlock($room_id, $micro_id)
    {
        $data = [
            "k" => 30012,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦设置老板位通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_micro_setboss($room_id, $micro_id)
    {
        $data = [
            "k" => 30013,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 普通麦取消老板位通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_micro_unsetboss($room_id, $micro_id)
    {
        $data = [
            "k" => 30014,
            "mid" => intval($micro_id),
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦闭麦指令
     * @param $room_id
     * @param $micro_id
     */
    public function command_micro_volume_close($room_id, $micro_id, $self = 0)
    {
        $data = [
            "k" => 30015,
            "mid" => intval($micro_id),
            "vself" => intval($self)
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦闭麦通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_micro_volume_close($room_id, $micro_id, $self = 0)
    {
        $data = [
            "k" => 30016,
            "mid" => intval($micro_id),
            "vself" => intval($self)
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 普通麦开麦指令
     * @param $room_id
     * @param $micro_id
     */
    public function command_micro_volume_open($room_id, $micro_id, $self = 0)
    {
        $data = [
            "k" => 30017,
            "mid" => intval($micro_id),
            "vself" => intval($self)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 普通麦开麦通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_micro_volume_open($room_id, $micro_id, $self = 0)
    {
        $data = [
            "k" => 30018,
            "mid" => intval($micro_id),
            "vself" => intval($self)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 添加排麦通知
     * @param $room_id
     * @param $user_id
     * @param $guard_type
     */
    public function broadcast_micro_waitbit_add($room_id, $user_id, $guard_type)
    {
        $user = $this->model_user->get_user($user_id);
        $uid = $user_id;
        $nn = $user['nickname'];
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
            if (isset($user["thumbnails"]) && count($user["thumbnails"]) > 0) {
                $img = $user["thumbnails"][0];
            }
        }
        $sex = $user["gender"];
        $bt = $user["birthday"];
        $v = $user['v'] ?? 0;
        $gs = [];
        if ($guard_type > 0) {
            $modelGuard = new Guard();
            $modelRoomHostMicro = new RoomHostMicro();
            $hostMicroUserId = $modelRoomHostMicro->get_user_id($room_id);
            if (!empty($hostMicroUserId)) {
                $guards = $modelGuard->getGuardDataCache($user_id, $hostMicroUserId);
                foreach ($guards as $guard) {
                    $gs[] = [
                        'expire_time' => $guard['expire_time'],
                        'name' => $guard['name'],
                        'type' => $guard['type'],
                        'guard_img' => $guard['guard_img'],
                        'guard_small_img' => $guard['guard_small_img']
                    ];
                }
            }
        }

        // 查询用户勋章
        $medal = $this->medalModel->getMedalUserListCache([$user_id], false);
        $medal = $medal[0] ?? [];

        $wallet = $this->model_finance_user_wallet->get_wallet($user_id);

        $data = [
            "k" => 30040,
            "uid" => $uid,
            "nn" => $nn,
            "img" => $img,
            "sex" => $sex,
            "bt" => $bt,
            "gt" => $guard_type,
            'gs' => $gs,
            'medal' => $medal,
            'v' => $v,
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),

        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 删除排麦通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_micro_waitbit_del($room_id, $user_id)
    {
        $data = [
            "k" => 30041,
            "uid" => intval($user_id)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 清空排麦通知
     * @param $room_id
     */
    public function broadcast_micro_waitbit_clean($room_id)
    {
        $data = [
            "k" => 30042
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 背景更换通知
     * @param $room_id
     * @param $url
     */
    public function broadcast_change_background($room_id, $url)
    {
        $data = [
            "k" => 30043,
            "url" => $url
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 用户离开房间指令
     * @param $room_id
     * @param $user_id
     */
    public function command_user_out($room_id, $user_id, $shotoff_time = 0)
    {
        $data = [
            "k" => 30045,
            "uid" => intval($user_id)
        ];
        if (!empty($shotoff_time)) {
            $data['sot'] = intval($shotoff_time);
        }
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 设置黑名单通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_block($room_id, $user_id)
    {
        $data = [
            "k" => 30046,
            "uid" => intval($user_id)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 取消黑名单通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_unblock($room_id, $user_id)
    {
        $data = [
            "k" => 30047,
            "uid" => intval($user_id)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 进入房间通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_user_into($room_id, $user_id)
    {
        //版本功能限制
        $ua = parse_app_UA();
        if(!empty($ua['version_no']) && !empty($ua['channel_id']) && !empty($ua['os_name'])) {
            $versionIntercept = [];
            if($ua['os_name'] == 'Android') {
                $versionIntercept = $this->modelSystem->getJsonArrConfigByStr('android_version_intercept');
            }
            if($ua['os_name']=='iPhone' || $ua['os_name']=='iPad') {
                $versionIntercept = $this->modelSystem->getJsonArrConfigByStr('ios_version_intercept');
            }
            $stop = 0;
            foreach($versionIntercept as $val) {
                if(isset($val['version']) && $val['version']===$ua['version_no']) {
                    if(empty($val['channel_id'])) {
                        $stop = 1;
                        break;
                    }else{
                        if($val['channel_id'] == $ua['channel_id']) {
                            $stop = 1;
                            break;
                        }
                    }
                }
            }
            if($stop == 1) {
                return false;
            }
        }

        $attires = $this->model_mall->getGoodsAttire($user_id);
        $rid = null;
        $rtype = null;
        $rres = null;
        $rresMd5 = null;
        $vapc_json_url = null;
        $is_new_mount = 0;
        $text_color = 'FFFFFF';
        for ($i = 0; $i < count($attires); $i++) {
            if ($attires[$i]['goods_type'] == 2) {
                $rid = $attires[$i]['goods_id'];
                $rtype = $attires[$i]['goods_type'];
                $rres = $attires[$i]['animate_resource'];
                $vapc_json_url = $attires[$i]['vapc_json_url'];
                $rresMd5 = $attires[$i]['animate_resource_md5'] ?? null;
                $is_new_mount =  $attires[$i]['is_new_mount'] ?? 0;
                $text_color = $attires[$i]['text_color'] ?? 0;
            }
        }

        $user = $this->model_user->get_user($user_id);
        $wallet = $this->model_finance_user_wallet->get_wallet($user_id);
        $room_user_count = $this->model_room_user->get_users_count($room_id);
        $uid = $user_id;
        $nn = $user['nickname'];
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
        }
        $sex = $user["gender"];
        $bt = $user["birthday"];

        $liang = 0;
        if (isset($user['liang'])) {
            $liang = $user['liang'];
        }
        $super = 0;
        if (isset($user['super'])) {
            $super = $user['super'];
        }
        $official = 0;
        if (isset($user['official'])) {
            $official = $user['official'];
        }
        $waiter = 0;
        if (isset($user['waiter'])) {
            $waiter = $user['waiter'];
        }
        $novice = 0;
        if (isset($user['novice'])) {
            $novice = $user['novice'];
        }
        $v = $user['v'] ?? 0;
        // 查询用户勋章
        $medal = $this->medalModel->getMedalUserListCache([$user_id], false);
        $medal = $medal[0] ?? [];

        //查询是否使用神秘面具道具
        $propUseInfo = $this->userPropModel->getPropUseTime($user_id, 15);

        // 获取魅力值
        $charmValue = $this->modelFinanceCharm->get_my_chart(3, $user_id);

        $data = [
            "k" => 30048,
            "uid" => $uid,
            "nn" => $nn,
            "img" => $img,
            "sex" => $sex,
            "wc" => intval($wallet['wealth_balance']),
            "bt" => $bt,
            "rid" => $rid,
            "rtype" => $rtype,
            "rres" => $rres,
            "vapc_json_url" => $vapc_json_url,
            "rmd5" => $rresMd5,
            "is_new_mount" =>$is_new_mount,
            "text_color" => $text_color,
            'ruc' => $room_user_count,
            "l" => $liang,
            "s" => $super,
            "o" => $official,
            "w" => $waiter,
            'nov' => $novice,
            'medal' => $medal,
            'v' => $v,
            'tcolor' => '13E0A6',
            'cv' => $charmValue,
            'cc' => intval($wallet['charm_balance']),
            "gameb" => empty(intval($wallet['game_balance'])) ? 0 : intval($wallet['game_balance']),
        ];

        //神秘人处理
        if ($propUseInfo) {
            $data['nn'] = "神秘人";
            $data['tcolor'] = "FF6AC4";
            unset($data['rid']);
            unset($data['rres']);
            unset($data['rmd5']);
            unset($data['medal']);
        }

        $this->send_room_msg($room_id, $data);

        //座驾
        $flag = 0;
        for ($i = 0; $i < count($attires); $i++) {
            if ($attires[$i]['goods_type'] == 7) {
                $rid = $attires[$i]['goods_id'];
                $rtype = $attires[$i]['goods_type'];
                $rres = $attires[$i]['animate_resource'];
                $vapc_json_url = $attires[$i]['vapc_json_url'];
                $rresMd5 = $attires[$i]['animate_resource_md5'] ?? null;
                $is_new_mount =  $attires[$i]['is_new_mount'] ?? 0;
                $text_color = $attires[$i]['text_color'] ?? 0;
                $flag = 1;
            }
        }
        if($flag == 1) {
            $data2 = $data;
            $data2['k'] = 30151;
            $data2['rid'] = $rid;
            $data2['rtype'] = $rtype;
            $data2['rres'] = $rres;
            $data2['vapc_json_url'] = $vapc_json_url;
            $data2['rresMd5'] = $rresMd5;
            $data2['is_new_mount'] = $is_new_mount;
            $data2['text_color'] = $text_color;

            if (empty($propUseInfo)) {
                $this->send_room_msg($room_id, $data2);
            }
        }
    }

    /**
     * 离开房间通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_user_out($room_id, $user_id)
    {
        $user = $this->model_user->get_user($user_id);
        $room_user_count = $this->model_room_user->get_users_count($room_id);
        $data = [
            "k" => 30049,
            "uid" => intval($user_id),
            "ruc" => $room_user_count
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 房间图片编辑通知
     * @param $room_id
     * @param $image
     */
    public function broadcast_edit_image($room_id, $image)
    {

        $data = [
            "k" => 30050,
            "img" => $image
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 房间名称编辑通知
     * @param $room_id
     * @param $name
     */
    public function broadcast_edit_name($room_id, $name)
    {

        $data = [
            "k" => 30051,
            "n" => $name
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 房间公告编辑通知
     * @param $room_id
     * @param $notice
     */
    public function broadcast_edit_notice($room_id, $notice)
    {
        //审核时不发消息
        $switch = $this->modelSystem->getExamineSwitch('switch');
        if($switch ==1){
            return;
        }
        $data = [
            "k" => 30052,
            "n" => $notice
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 房间管理员设置通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_role($room_id, $user_id)
    {

        $data = [
            "k" => 30053,
            "uid" => intval($user_id)
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 房间管理员取消通知
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_unrole($room_id, $user_id)
    {

        $data = [
            "k" => 30054,
            "uid" => intval($user_id)
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 设置主持麦礼物通知
     * @param $room_id
     * @param $micro_id
     * @param $gift_id
     * @param $gift_num
     */
    public function broadcast_hostmicro_gift($send_user_id, $room_id, $micro_id, $gift_id, $gift_num)
    {
        $user = $this->model_user->get_user($send_user_id);
        $uid = $user['user_id'];
        $nn = $user['nickname'];
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
        }

        $giftData = $this->modelGift->formatGiftInfo($this->modelGift->get_gift($gift_id), $gift_num, $send_user_id);
        $data = [
            "k" => 30055,
            "gid" => intval($gift_id),
            "gc" => intval($gift_num),
            "mid" => intval($micro_id),
            "uid" => $uid,
            "nn" => $nn,
            "img" => $img,
            'gift_name' => $giftData['name'] ?? '',
            'gift_image' => $giftData['image'] ?? '',
            'gift_resource' => $giftData['animate_resource'] ?? '',
            'gift_resource_md5' => $giftData['animate_resource_md5'] ?? '',
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 取消主持麦礼物通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_hostmicro_ungift($room_id, $micro_id)
    {
        $data = [
            "k" => 30056,
            "mid" => intval($micro_id)
        ];
        $this->send_room_msg($room_id, $data);
    }



    /**
     * 取消主持麦小屋礼物通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcastHostMicroPearlUngift($room_id, $micro_id)
    {
        $data = [
            "k" => 30071,
            "mid" => intval($micro_id)
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 设置麦礼物通知
     * @param $room_id
     * @param $micro_id
     * @param $gift_id
     * @param $gift_num
     */
    public function broadcast_micro_gift($send_user_id, $room_id, $micro_id, $gift_id, $gift_num)
    {
        $user = $this->model_user->get_user($send_user_id);
        $uid = $user['user_id'];
        $nn = $user['nickname'];
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
        }

        $giftData = $this->modelGift->formatGiftInfo($this->modelGift->get_gift($gift_id), $gift_num, $send_user_id);
        $data = [
            "k" => 30057,
            "gid" => intval($gift_id),
            "gc" => intval($gift_num),
            "mid" => intval($micro_id),
            "uid" => $uid,
            "nn" => $nn,
            "img" => $img,
            'gift_name' => $giftData['name'] ?? '',
            'gift_image' => $giftData['image'] ?? '',
            'gift_resource' => $giftData['animate_resource'] ?? '',
            'gift_resource_md5' => $giftData['animate_resource_md5'] ?? '',
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 取消麦礼物通知
     * @param $room_id
     * @param $micro_id
     */
    public function broadcast_micro_ungift($room_id, $micro_id)
    {
        $data = [
            "k" => 30058,
            "mid" => intval($micro_id)
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 普通麦抱上麦指令
     * @param $room_id
     * @param $micro_id
     * @param $user_id
     */
    public function command_micro_into_req($room_id, $micro_id, $user_id)
    {
        $data = [
            "k" => 30061,
            "mid" => intval($micro_id),
            "uid" => intval($user_id),
        ];
        $this->send_room_msg($room_id, $data);

        $this->model_room->addMicroLog($room_id, $micro_id, $user_id,1);
    }


    /**
     * 房间内设置头饰
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_update_coronet($room_id, $user_id)
    {
        $attires = $this->model_mall->getGoodsAttire($user_id);
        $cid = null;
        $cres = null;
        for ($i = 0; $i < count($attires); $i++) {
            if ($attires[$i]['goods_type'] == 1) {
                $cid = $attires[$i]['goods_id'];
                $cres = $attires[$i]['animate_resource'];
            }
        }
        if (empty($cid)) {
            return;
        }
        $data = [
            "k" => 30062,
            "uid" => $user_id,
            "cid" => $cid,
            "cres" => $cres
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 房间内取消设置头饰
     * @param $room_id
     * @param $user_id
     */
    public function broadcast_cancel_coronet($room_id, $user_id)
    {
        $data = [
            "k" => 30063,
            "uid" => $user_id
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 设置聊天屏幕通知
     * @param $room_id
     * @param $opened
     */
    public function broadcast_screen_opened($room_id, $opened)
    {
        $data = [
            "k" => 30064,
            "opened" => $opened
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 设置密码通知
     * @param $room_id
     * @param $password
     */
    public function broadcast_password($room_id, $password)
    {
        $pwd = 0;
        if ($password !== null) {
            $pwd = 1;
        }
        $data = [
            "k" => 30065,
            "pwd" => $pwd
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 房间关闭指令
     * @param $room_id
     */
    public function command_close($room_id)
    {
        $data = [
            "k" => 30066,
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 设置全麦是否关闭通知
     * @param $room_id
     * @param $closed
     */
    public function broadcast_volume_closed($room_id, $closed)
    {
        $data = [
            "k" => 30067,
            "closed" => intval($closed)
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 通知房间热度发生变化
     * @param $room_id
     */
    public function broadcast_hot_change($room_id)
    {
        $hot = $this->model_room_hot->get($room_id);
        $data = [
            "k" => 30068,
            "hot" => $hot
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 发送表情
     * @param $room_id
     */
    public function broadcastExp($roomId, $userId, $resId)
    {
        $user = $this->model_user->get_user($userId);
        $wallet = $this->model_finance_user_wallet->get_wallet($userId);

        $liang = 0;
        if (isset($user['liang'])) {
            $liang = $user['liang'];
        }
        $waiter = 0;
        if (isset($user['waiter'])) {
            $waiter = $user['waiter'];
        }
        $novice = 0;
        if (isset($user['novice'])) {
            $novice = $user['novice'];
        }
        $img = null;
        if (count($user["images"]) > 0) {
            $img = $user["images"][0];
        }

        //用户铭牌
        $np = [];
        $userNameplate = $this->nameplateService->getNameplateByUserId($userId);
        foreach($userNameplate as $vn) {
            if(!empty($vn['nameplate_resource'])) {
                $np[] = $vn['nameplate_resource'];
                continue;
            }
            if(!empty($vn['nameplate_img'])) {
                $np[] = $vn['nameplate_img'];
                continue;
            }
        }

        $data = [
            "k" => 30044,
            'uid' => $userId,
            'eid' => $resId,
            'nn' => $user['nickname'],
            'sex' => $user['gender'],
            'bt' => $user['birthday'],
            'mlt' => 1, //标记是多图表情
            'pro' => 1,
            'sm' => 1,
            'l' => $liang,
            'w' => $waiter,
            'img' => $img,
            //'bres' => null,
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),
            "rc" => intval($wallet['reputation_balance']),
            "bc" => intval($wallet['celebrity_balance']),
            'nov' => $novice,
            'medal' => [],
            'np' => $np,
        ];
        $this->send_room_msg($roomId, $data);
    }


    /**
     * 发送房间消息
     * @param $room_id
     * @param $data
     */
    public function send_room_msg($room_id, $data)
    {
        $res = $this->lib_rong->send_room_msg($room_id, $data);
        return $res;
    }


    /**
     * 保活融云房间
     * @param $roomId
     */
    public function keepRongCloud($roomId)
    {
        $data = [
            "k" => 0,
        ];
        $this->lib_rong->send_room_msg($roomId, $data);
    }

    /**
     * 发送心动状态 0.未开启(关闭) 1.开启(重开) 2.选择 3.公布
     * @param $roomId
     * @param $state
     * @param $time
     */
    public function broadcastHeartState($roomId, $state, $time = null)
    {
        $data = [
            'k' => 30019,
            'state' => intval($state)
        ];
        if (!is_null($time)) {
            $data['select_time'] = $time;
        }
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 发布选择
     * @param $roomId
     * @param $userId
     */
    public function broadcastHeartSelected($roomId, $userId)
    {
        $data = [
            'k' => 30023,
            'user_id' => $userId
        ];
        $this->send_room_msg($roomId, $data);
    }


    /**
     * 心动发布选择
     * @param $roomId
     * @param $selectData
     */
    public function broadcastHeartSelect($roomId, $selectData)
    {
        $data = [
            'k' => 30020,
            'select_data' => $selectData
        ];
        $this->send_room_msg($roomId, $data);
    }


    /**
     * 发送计数状态 0.未开启(关闭) 1.开启(重开)
     * @param $roomId
     * @param $opened
     */
    public function broadcastTallyState($roomId, $opened)
    {
        $data = [
            'k' => 30021,
            'opened' => intval($opened)
        ];
        $this->send_room_msg($roomId, $data);
    }


    /**
     * 发送计数变更
     * @param $roomId
     * @param $tallyData
     * @param $maxSendTallyData
     */
    public function broadcastTallyData($roomId, $tallyData)
    {
        $data = [
            'k' => 30022,
            'tally_data' => $tallyData,
        ];
        $this->send_room_msg($roomId, $data);
    }


    /**
     * 发送主题变更消息
     * @param $roomId
     * @param $topic
     */
    public function broadcastChangeTopic($roomId, $topic)
    {
        $data = [
            'k' => 30074,
            'topic' => $topic
        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 房间type_id编辑通知
     * @param $room_id
     * @param $type_id
     */
    public function broadcast_edit_type_id($room_id, $type_id, $game_area = "", $game_level = "")
    {
        $roomType = $this->model_room->getRoomType($type_id);
        $typeName = ($roomType && isset($roomType['type_name'])) ? $roomType['type_name'] : "新模式";
        $gameArea = explode(",", $game_area);
        $gameLevel = explode(",", $game_level);

        $data = [
            "k" => 30086,
            "type_id" => $type_id,
            "type_name" => $typeName,
            "text" => "房间模式已变更为",
            "game_area" => $gameArea,
            "game_level" => $gameLevel,
            "default_background_pic" => $roomType['default_background_pic'],
            "app_url" => $roomType['app_url'],
            "timestamp" => time()
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 切换排麦方式消息通知
     * @param array
     * */
    public function changeMicroWayMsg($params)
    {
        $data = [
            'k' => 30101,
            'room_id' => $params['room_id'],
            'type' => $params['type'],
            'created_at' => time(),
        ];

        $this->send_room_msg($data['room_id'], $data);
    }

    /**
     * 清空公屏消息
     * @param array
     * */
    public function clearScreenMsg($params)
    {
        $data = [
            'k' => 30102,
            'room_id' => $params['room_id'],
            'created_at' => time(),
        ];

        $this->send_room_msg($data['room_id'], $data);
    }

    /**
     * xxx 收藏了房间消息
     * @param int $userId 用户ID
     * @param int $roomId 房间ID
     * @return mixed
     * */
    public function collectRoomMsg($userId, $roomId)
    {
        $getUser = $this->model_user->get_user($userId);
        if (empty($getUser)) {
            return false;
        }
        $is_collect = $this->model_room->get_collect($roomId ,$userId);
        if($is_collect){
            return false;
        }
        $data = [
            'k' => 30097,
            'room_id' => $roomId,
            'user_id' => $userId,
            'nickname' => $getUser['nickname'],
            'image' => $getUser['images'][0] ?? '',
            'created_at' => time(),
        ];

        $this->send_room_msg($roomId, $data);
    }

    /**
     * xxx 欢迎xxx消息
     * @param int $fromUserId 来自欢迎用户ID
     * @param int $toUserId 被欢迎用户ID
     * @param int $roomId 房间ID
     * @return mixed
     * */
    public function welcomeUserMsg($fromUserId, $toUserId, $roomId)
    {
        $getFromUser = $this->model_user->get_user($fromUserId);
        if (empty($getFromUser)) {
            return false;
        }

        $getToUser = $this->model_user->get_user($toUserId);
        if (empty($getToUser)) {
            return false;
        }

        $textArray = [
            '小可爱你可来啦',
            '欢迎小可爱回家',
            '恭喜你找到组织啦~',
            '欢迎加入，上麦(｡･∀･)ﾉﾞ嗨啊',
            '终于等到你，还好没放弃~',
            '54321，我们一起嘿嘿嘿',
        ];
        shuffle($textArray);
        $text = end($textArray);

        $wallet = $this->model_finance_user_wallet->get_wallet($fromUserId);

        $data = [
            'k' => 30098,
            'room_id' => $roomId,
            'from_user' => [
                'user_id' => $fromUserId,
                'nickname' => $getFromUser['nickname'],
                'image' => $getFromUser['images'][0] ?? '',
            ],
            'to_user' => [
                'user_id' => $toUserId,
                'nickname' => $getToUser['nickname'],
                'image' => $getToUser['images'][0] ?? '',
            ],
            'text' => $text,
            'created_at' => time(),
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),
        ];

        $this->send_room_msg($roomId, $data);
    }



    /**
     * sendRoomHotCallMsg 发送房间热度消息
     * @param int $roomId 房间ID
     * @return mixed
     * */
    public function sendRoomHotCallMsg($roomId, $status, $time, $ttl = 600)
    {
        $data = [
            'k' => 30103,
            'room_id' => $roomId,
            'status' => $status,
            'ttl' => $ttl,
            'time' => $time,
            'created_at' => time(),
        ];

        $this->send_room_msg($roomId, $data);
    }

    /**
     * 发送图片消息
     * @param $room_id
     * @param $send_user_id
     * @param $recv_user_ids
     * @param $gift_id
     * @param $gift_count
     */
    public function sendImgMsg($room_id, $send_user_id, $img_url)
    {

        $send_user = $this->model_user->get_user($send_user_id);
        $wallet = $this->model_finance_user_wallet->get_wallet($send_user_id);
        $room_wealth_value = $this->model_finance_room_wallet->get_wallet($room_id);


        $liang = $send_user['liang'] ?? 0;
        $super = $send_user['super'] ?? 0;
        $official = $send_user['official'] ?? 0;
        $waiter = $send_user['waiter'] ?? 0;
        $novice = $send_user['novice'] ?? 0;
        $v = $send_user['v'] ?? 0;

        if (count($send_user["images"]) > 0) {
            $fimg = $send_user["images"][0];
            if (isset($send_user["thumbnails"]) && count($send_user["thumbnails"]) > 0) {
                $fimg = $send_user["thumbnails"][0];
            }
        }

        // 查询用户勋章
        $medal = $this->medalModel->getMedalUserListCache([$send_user_id], false);
        $medal = $medal[0] ?? [];

        $modelGuard = new Guard();
        $modelRoomHostMicro = new RoomHostMicro();
        $hostMicroUserId = $modelRoomHostMicro->get_user_id($room_id);
        $gs = null;
        if (!empty($hostMicroUserId)) {
            $guards = $modelGuard->getGuardDataCache($send_user['user_id'], $hostMicroUserId);
            $gs = $guards[0] ?? null;
        }

        $data = [
            "k" => 30107,
            "fid" => intval($send_user_id),
            "fnn" => $send_user['nickname'],
            "fimg" => $fimg,
            "fsex" => $send_user['gender'],
            "fbt" => $send_user["birthday"],
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),
            "rc" => intval($wallet['reputation_balance']),
            "bc" => intval($wallet['celebrity_balance']),
            "rwc" => $room_wealth_value,
            "l" => $liang,
            "s" => $super,
            "o" => $official,
            "w" => $waiter,
            "v" => $v,
            'nov' => $novice,
            'medal' => $medal,
            'gsm' => $gs,
            'img_url'=>$img_url
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**设置用户音乐权限消息
     * @param $room_id
     * @param $user_id
     * @param $type
     */
    public function send_music_permission($room_id ,$user_id ,$type){
        $data = [
            "k" => 30126,
            'user_id'=>$user_id ,
            'type'=>$type,
            'time' =>time()
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**播放音乐消息
     * @param $room_id
     * @param $user_id
     */
    public function send_music_play($room_id ,$user_id ){
        $data = [
            "k" => 30127,
            'user_id'=>$user_id ,
            'time' =>time()
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 发送神豪榜更新消息
     * @param $room_id int 房间id
     * @param $vip_user_id int 神豪id
     * @param $mvp_user_id int 守护id
     * @param $vip_score int 榜单值
     * @param $level int 等级
     * @return false|void
     */
    public function roomChampionUpdate($room_id, $vip_user_id, $mvp_user_id, $vip_score, $level){

        //历史参数，新版本中已经没用，但兼容老版本，需要保留。1:财富榜(神豪) 2:魅力榜 3:0点清空
        $type = empty($vip_user_id) && empty($mvp_user_id) ? 3 : 1;
        $data = [
            'k' => 30129,
            'vip_mvp_level' => $level,
            'wealth_user' => null, //财富榜(神豪)
            'charm_user' => null , //魅力榜
            'type' =>$type,
        ];
        if($vip_user_id > 0){
            $user = $this->model_user->get_user($vip_user_id);
            if($user){
                $data['wealth_user'] = [
                    'user_id'=>$user['user_id'] ,
                    'nickname'=>$user['nickname'],
                    'portrait'=>$user['images'][0],
                    'value' =>$vip_score
                ];
            }
        }

        if($mvp_user_id > 0){
            $user = $this->model_user->get_user($mvp_user_id);
            if($user){
                $data['charm_user'] = [
                    'user_id'=>$user['user_id'] ,
                    'nickname'=>$user['nickname'],
                    'portrait'=>$user['images'][0],
                    'value' =>$vip_score
                ];
            }
        }

        $this->send_room_msg($room_id, $data);
    }

    /**
     * 神豪位开关消息
     */
    public function godHaoSwitchMsg(int $roomId = 0, int $open = 0){
        $data = [
            'k' => 30130,
            'o' => $open,
        ];
        $this->send_room_msg($roomId, $data);
    }


    /**
     * 房间开关消息
     * room
     * type 开关类型
     * open 开关状态 1开启
     */
    public function sendRoomSwitchMsg(int $roomId = 0, string $switch_key='', int $switch_value = 0){
        $data = [
            'k' => 20200,
            't' => $switch_key,
            'o' => $switch_value,
        ];

        $this->send_room_msg($roomId, $data);
    }

    /**
     * 用户发起游戏推送消息
     *
     * @param integer $roomId
     * @param integer $userId
     * @param integer $sponsorUserId
     * @param string $gameName
     * @return void
     */
    public function sendCustomGameLaunchMsg(int $roomId, int $userId, int $sponsorUserId, string $gameName){
        $sponsorUser = $this->model_user->get_user($sponsorUserId);
        $user = $this->model_user->get_user($userId);
        $data = [
            'k' => 30141,
            'name' => $user['nickname'],
            't_name' => $sponsorUser['nickname'],
            'game' => $gameName,
            'txt' => "互动提示：{{name}}向{{t_name}}发起了{{game}}互动",
        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 涉黄违规警告消息
     * @param $room_id int 房间ID
     * @param $text string 消息内容
     * @param $user_id int 涉黄者用户ID
     */
    public function pronWarningMsg($room_id, $text, $user_id)
    {
        $data = [
            "k" => 10010,
            "t" => $text,
            "tid" => $user_id
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 发起房间投票
     * @param int $roomId
     * @param array $data 投票数据体
     * @return bool
     */
    public function sendStartRoomVote($roomId,$data)
    {
        $data['k'] = 80030;
        $data['current_time'] = time();
        $data['from_user_vote_num'] = $data['vote_num'];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 结束房间投票
     * @param int $roomId
     * @param array $data 投票数据体
     * @param string $msg 广播的结束消息
     * @return bool
     */
    public function sendEndRoomVote($roomId,$data,$msg)
    {
        $data['k'] = 80031;
        $data['t'] = $msg;
        $data['current_time'] = time();
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 取消房间投票
     * @param int $roomId
     * @param int $voteId 投票ID
     * @param string $msg 广播的取消消息
     * @return bool
     */
    public function sendCancelRoomVote($roomId,$voteId,$msg)
    {
        $data = [
            'k' => 80032,
            'vote_id' => $voteId,
            't' => $msg,

        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 房间投票 - 用户投票
     * @param int $userId
     * @param array $data 数据体
     * @param int $voteNum 投票者的剩余票数
     * @return void
     */
    public function sendPollRoomVote($userId, $data, $voteNum)
    {
        $data['k'] = 80033;
        $data['from_user_id'] = $userId;
        $data['from_user_vote_num'] = $voteNum;
        $this->send_room_msg($data['room_id'], $data);
    }

    /**
     * 发送房间管理员任命消息
     * @param int $roomId 房间id
     * @param int $type 0取消任命 1任命
     * @param int $toUserId 任命(取消)谁
     * @param int $list 管理员列表
     * @return void
     */
    public function sendRoomRoleListMsg($roomId, $type, $toUserId)
    {
        //读取房间的管理员信息
        $room_item = $this->model_room->get_room($roomId);
        $admins = $this->model_room->get_roles_limit($roomId,0,100);
        foreach ($admins as &$a) {
            if ($a['role_type'] == 2) { //房主
                $a['is_super'] = $a['user_id'] == $room_item['user_id'] ? 0 : 1; //检查超管
            } else {
                $a['is_super'] = 0;
            }
        }

        $data = [
            'k' => 80034,
            't' => $type,
            'to_user_id' => $toUserId,
            'list' => $admins,
        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 全家总动员 用户押注
     */
    public function fcSendStack($room_id, $data)
    {
        $data = [
            "k" => 60101,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 全家总动员 回合信息
     */
    public function fcSendRound($room_id, $data)
    {
        $data = [
            "k" => 60102,
            "data" => $data
        ];
        //一起玩消息通知
        $togetherGame = [
            "k" => 60104,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
        //一起玩消息通知
        $this->send_room_msg($room_id,$togetherGame);
    }

    /**
     * 西天取经 悬赏开始
     */
    public function xtqjSendOffer($room_id, $data)
    {
        $data = [
            "k" => 60201,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 西天取经 公告
     */
    public function xtqjSendNotice($room_id, $data)
    {
        $data = [
            "k" => 60202,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 西天取经 悬赏差的次数
     */
    public function xtqjSendOfferNeedNum($room_id, $data)
    {
        $data = [
            "k" => 60203,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 西天取经 悬赏开启关闭
     */
    public function xtqjSendOfferStatus($room_id, $data)
    {
        $data = [
            "k" => 60204,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }


    /**
     * 房间模式编辑通知
     * @param $room_id
     * @param $room_mode
     */
    public function roomModeMsg($room_id, $room_mode, $paramData=null)
    {
        $data = [
            "k" => 30142,
            "room_mode" => $room_mode,
        ];
        if(!empty($paramData)) {
            $data['data'] = $paramData;
        }
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 房间团战通知
     * @param $roomId
     * @param $data
     */
    public function roomPkMsg($roomId, $status, $data)
    {
        $data = [
            "k" => 30143,
            "status" => $status,
            "data" => $data
        ];

        $this->send_room_msg($roomId, $data);
    }

    /**
     * 幸运礼物消息
     */
    public function luckGiftMsg($room_id, $send_user_id, $gift_id, $gift_count, $lgm)
    {
        $send_user = $this->model_user->get_user($send_user_id);
        $wallet = $this->model_finance_user_wallet->get_wallet($send_user_id);
        $room_wealth_value = $this->model_finance_room_wallet->get_wallet($room_id);
        $giftData = $this->modelGift->formatGiftInfo($this->modelGift->get_gift($gift_id), $gift_count, $send_user_id);

        $liang = $send_user['liang'] ?? 0;
        $super = $send_user['super'] ?? 0;
        $official = $send_user['official'] ?? 0;
        $waiter = $send_user['waiter'] ?? 0;
        $novice = $send_user['novice'] ?? 0;
        $v = $send_user['v'] ?? 0;

        $fimg = null;
        if (count($send_user["images"]) > 0) {
            $fimg = $send_user["images"][0];
            if (isset($send_user["thumbnails"]) && count($send_user["thumbnails"]) > 0) {
                $fimg = $send_user["thumbnails"][0];
            }
        }

        $data = [
            "k" => 20008,
            "fid" => intval($send_user_id),
            "fnn" => $send_user['nickname'],
            "fimg" => $fimg,
            "fsex" => $send_user['gender'],
            "fbt" => $send_user["birthday"],
            // "tu" => $recvs,
            "gid" => intval($gift_id),
            // "flag" => $flag,
            "gc" => intval($gift_count),
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),
            "rc" => intval($wallet['reputation_balance']),
            "bc" => intval($wallet['celebrity_balance']),
            "rwc" => $room_wealth_value,
            // "at" => $at,
            "l" => $liang,
            "s" => $super,
            "o" => $official,
            "w" => $waiter,
            "v" => $v,
            'nov' => $novice,
            // 'medal' => $medal,
            // 'gsm' => $gs,
            // 'w_incr' => $wealthTotal,
            'gift_name' => $giftData['name'],
            'gift_image' => $giftData['image'] ?? '',
            'gift_resource' => $giftData['animate_resource'] ?? '',
            'gift_resource_md5' => $giftData['animate_resource_md5'] ?? '',
            // 'te' => $isSpecialGift                                      // 1 特殊礼物
            'lgm' => $lgm,//幸运礼物倍数累加值
        ];

        if(!empty($track_json)){
            $data['track_json'] = $track_json;
        }
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 幸运礼物消息
     */
    public function luckGiftMsgV2($roomId, $fromUserId, $giftName, $giftNum, $diamondNum, $nickname, $multiple = '', $is_sound = '0')
    {
        $data = [
            "k" => 20011,
            "fid" => intval($fromUserId),
            "fnn" => $nickname,
            'gift_name' => $giftName,
            'gc' => $giftNum,
            'diamond_num' => $diamondNum,
            'multiple' => $multiple,
            'is_sound' => $is_sound,
        ];

        $this->send_room_msg($roomId, $data);
    }

    /** 
    * 礼物彩蛋通知
    */
    public function colorGiftMsg($room_id, $user_id, $gift_id, $gift_num, $roomInfo, $allGiftData, $colorGiftData){
        $send_user = $this->model_user->get_user($user_id);
        $wallet = $this->model_finance_user_wallet->get_wallet($user_id);
        $room_wealth_value = $this->model_finance_room_wallet->get_wallet($room_id);

        //处理数据
        $finalData = [];
        foreach($allGiftData as $k=>$v){
            $finalData[] = [
                'gift_id' => $v['pop_gift_id'],
                'gift_count' => $v['pop_num'],
            ];
        }
        

        $giftInfos = [];
        $specialScreen = [];
        $position = config('animation.position');
        $flag = 0;
        foreach($finalData as $f){
            $giftData = $this->modelGift->formatGiftInfo($this->modelGift->get_gift($f['gift_id']), $f['gift_count'], $user_id);
            
            //判断获得的礼物中是否有大额礼物发送公屏飘屏(爆出价格大于50钻且大于原价的1倍)
            $comparePrice = 9900;
            if($giftData['price'] >= $position['b']){
                $giftInfos[] = [
                    'gift_price' => $giftData['price'],
                    'gift_name' => $giftData['name'].'('.$giftData['price'].'钻石)',
                    'gift_image' => $giftData['image'] ?? '',
                    'gift_resource' => $giftData['animate_resource'] ?? '',
                    'gift_resource_md5' => $giftData['animate_resource_md5'] ?? '',
                    'gift_num' => $f['gift_count']
                ];
                if($giftData['price'] > $comparePrice){
                    $specialScreen[] = [
                        'gift_price' => $giftData['price'],
                        'gift_name' => $giftData['name'].'('.$giftData['price'].'钻石)',
                        'gift_image' => $giftData['image'] ?? '',
                        'gift_resource' => $giftData['animate_resource'] ?? '',
                        'gift_resource_md5' => $giftData['animate_resource_md5'] ?? '',
                        'gift_num' => $f['gift_count']
                    ];
                    $id = 61;
                    //查询指定彩蛋特殊飘屏配置
                    $specialFullScreenInfo = $this->modelSystem->getFullScreen(67);
                    $roomIds = [];
                    if(!empty($specialFullScreenInfo)){
                        $roomIds = explode(',', $specialFullScreenInfo['room_ids']);
                    } 
                    if(!in_array($room_id, $roomIds)){
                        //$id = $specialFullScreenInfo['id'];
                        $iconUrl = '';
                        $content = $colorGiftData['name'].',获得'.$giftData['name'].'x'.$f['gift_count'];
                        $textParam = ['name'=>$send_user['nickname'], 'roomName'=>$roomInfo['name'], 'content'=> $content];
                        $actionParam = ['roomId'=>$room_id];
                        $this->MsgServie->sendFullScreen($id, $iconUrl, $textParam, $actionParam);
                    }   
                }
            }
        }
        
        $liang = $send_user['liang'] ?? 0;
        $super = $send_user['super'] ?? 0;
        $official = $send_user['official'] ?? 0;
        $waiter = $send_user['waiter'] ?? 0;
        $novice = $send_user['novice'] ?? 0;
        $v = $send_user['v'] ?? 0;

        if (count($send_user["images"]) > 0) {
            $fimg = $send_user["images"][0];
            if (isset($send_user["thumbnails"]) && count($send_user["thumbnails"]) > 0) {
                $fimg = $send_user["thumbnails"][0];
            }
        }
        
        $data = [
            "k" => 20009,
            "fid" => intval($user_id),
            "fnn" => $send_user['nickname'],
            "fimg" => $fimg,
            "fsex" => $send_user['gender'],
            "fbt" => $send_user["birthday"],
            "gid" => intval($gift_id),
            // "flag" => $flag,
            "gc" => intval($gift_num),
            "wc" => intval($wallet['wealth_balance']),
            "cc" => intval($wallet['charm_balance']),
            "rc" => intval($wallet['reputation_balance']),
            "bc" => intval($wallet['celebrity_balance']),
            "rwc" => $room_wealth_value,
            // "at" => $at,
            "l" => $liang,
            "s" => $super,
            "o" => $official,
            "w" => $waiter,
            "v" => $v,
            'nov' => $novice,
            'gift_name' => $colorGiftData['name'],
            'gift_image' => $colorGiftData['image'] ?? '',
            'gift_resource' => $colorGiftData['animate_resource'] ?? '',
            'gift_resource_md5' => $colorGiftData['animate_resource_md5'] ?? '',
            'gift_type' => empty($colorGiftData['gift_type']) ? 0 : $colorGiftData['gift_type'],
            'sendGift' => $giftInfos,//动画
        ];
        if(!empty($specialScreen)){
            $data['specialScreen'] = $specialScreen;//公屏
        }
        if(!empty($track_json)){
            $data['track_json'] = $track_json;
        }
        $res = $this->send_room_msg($room_id, $data);
        return $res;
    }

    /**
     * 荒漠捕牛 用户押注
     */
    public function lcSendStack($room_id, $data)
    {
        $data = [
            "k" => 60111,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 荒漠捕牛 回合信息
     */
    public function lcSendRound($room_id, $data)
    {
        $data = [
            "k" => 60112,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 盲盒礼物公屏消息
     */
    public function giftMh_msg($toUserIds, $userGift, $fromUserId, $roomId, $giftData)
    {
        $send_user = $this->model_user->get_user($fromUserId);
        $toUserList = $this->model_user->get_users_by_ids($toUserIds);
        $toUserList = array_column($toUserList, null, 'user_id');

        foreach($toUserIds as $toUserId) {
            if(empty($toUserList[$toUserId]) || empty($userGift[$toUserId])) {
                continue;
            }
            $msgData = [
                "k" => 20010,
                "fid" => $fromUserId,
                "fnn" => $send_user['nickname'],
                "tid" => (int) $toUserId,
                "tnn" => $toUserList[$toUserId]['nickname'],
                "gc" => $userGift[$toUserId]['gift_num'],
                "gift_value" => $userGift[$toUserId]['num'],
                "gift_name" => $giftData['name'],
            ];
            $this->send_room_msg($roomId, $msgData);
        }
    }

    /**
     * 开运装备星 公告
     */
    public function equipDrawSendNotice($room_id, $data)
    {
        $data = [
            "k" => 60205,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }




    /**
     * 三国群英 用户押注
     */
    public function glSendStack($room_id, $data)
    {
        $data = [
            "k" => 60113,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 三国群英 回合信息
     */
    public function glSendRound($room_id, $data)
    {
        $data = [
            "k" => 60114,
            "data" => $data
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 发送房间真爱榜通知消息开关状态
     */
    public function broadcastLoveRankData($room_id, $status){
        $data = [
            "k" => 30024,
            "love_rank_status" => $status
        ];
        $this->send_room_msg($room_id, $data);
    }

    /**
     * 房间锁麦消息
     */
    public function roomMicroLockMsg($roomId, $log, $status)
    {
        $roomInfo = $this->model_room->get_room($roomId);
        $data = [
            "k" => 30069,
            "micro_lock" => [
                "room_id" => $log['room_id'],
                "micro_id" => $log['micro_id'],
                "lock_user_id" => $log['lock_user_id'],
                "room_user_id" => $log['room_user_id'],
                "status" => $status,//1=锁麦 0=解锁
                "room_name" => empty($roomInfo['name']) ? '' : $roomInfo['name'],
            ],
        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 拍拍房通用消息
     */
    public function roomPaiPaiAddTimeMsg($roomId, $endTime, $status, $defineInfo=[])
    {
        if(empty($defineInfo)) {
            $defineInfo = (object) $defineInfo;
        }
        $data = [
            "k" => 30081,
            "paipai_info" => [
                "room_id" => $roomId,
                "status" => $status,
                "end_time" => $endTime,
                "define_info" => $defineInfo,
            ],
        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 拍拍房麦位消息
     */
    public function roomPaiPaiMicroMsg($roomId, $info, $micros)
    {
        $data = [
            "k" => 30082,
            "paipai_micro" => [
                "room_id" => $roomId,
                "info" => $info,
                "micros" => $micros,
            ],
        ];
        $this->send_room_msg($roomId, $data);
    }

    /**
     * 愤怒的小鸟 游戏信息
     */
    public function angerBirdMsg($room_id, $data)
    {
        $data = [
            "k" => 60115,
            "angerbird" => $data
        ];
        // write_log('angerbird-test', '游戏信息', $data);

        $this->send_room_msg($room_id, $data);
    }


}
